<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/6/17
 * Time: 9:25
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class Keeper extends Base {
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
      public function house(){
          extract(input());
          $where['xq_user_id'] = $user_id;
          $where['xq_sh_status'] = 2;
          $house_id = Db::table('up_xq_property')->where($where)->column('house_id');
          $map['a.house_id'] = array('IN',$house_id);
          $res =  Db::table('up_xq_house')->alias('a')
              ->join('up_xq ux','ux.xq_id = a.xq_id','left')
              ->join('up_xq_block uxb','uxb.block_id = a.block_id','left')
              ->join('up_xq_room uxr','uxr.room_id = a.room_id','left')
//              ->join('up_xq_place uxp','uxp.place_id = a.place_id','left')
              ->where($map)
              ->field('a.*,ux.xq_address,ux.xq_name,uxb.block_name,uxr.room_name')
              ->select();
          return json(['status' => 200, 'msg' =>$res]);
      }


      public function index(){
          extract(input());
          $type = input('param.type');
          $house_id= input('param.house_id');
          $page = input('param.page')?input('param.page'):1;
          $num = 10;
          $limit = ($page-1)*$num;
          $res = array();
          if($house_id!=''){
              $where['a.house_id'] = $house_id;
          }else{
              return json(['status' => 200, 'msg' =>$res]);
          }
          if($type==1){
            $where['pay_type'] =1;
          }elseif ($type==2){
             $where['pay_type'] =2;
          }elseif($type==3){
              $where['pay_type'] =3;
          }
         $res =  Db::table('up_xq_pay')->alias('a')
              ->join('up_xq_house uxh','uxh.house_id = a.house_id','left')
              ->join('up_xq ux','ux.xq_id = uxh.xq_id','left')
              ->join('up_xq_block uxb','uxb.block_id = uxh.block_id','left')
              ->join('up_xq_room uxr','uxr.room_id = uxh.room_id','left')
              ->join('up_xq_place uxp','uxp.place_id = a.place_id','left')
              ->where($where)
              ->field('a.*,ux.xq_name,uxb.block_name,uxr.room_name,uxp.place_name')
              ->limit($limit,$num)
              ->select();
          return json(['status' => 200, 'msg' =>$res]);
      }
     //缴费记录
     public function payList(){
         extract(input());
         $type = input('param.type');
         $page = input('param.page')?input('param.page'):1;
         $house_id = input('param.house_id');
         $num = 10;
         $limit = ($page-1)*$num;
         $where['pay_status'] = 2;
         $where['pay_user_id'] = $user_id;
         $where['pay_house_id'] = $house_id;
         if($type==1){//物业费
             $where['pay_cate'] =3;
         }elseif ($type==2){//水电费
             $where['pay_cate'] =array('IN',array(1,2));
         }elseif($type==3){//停车费
             $where['pay_cate'] =4;
         }elseif ($type==4){//租金缴纳
             $where['pay_cate'] =5;
         }
         $res = Db::table('up_pay')->alias('a')
             ->join('up_u_user uuu','uuu.user_id = a.pay_user_id','left')
             ->where($where)
             ->limit($limit,$num)
             ->field('uuu.user_username,a.pay_price,a.pay_time')
             ->order('a.pay_time desc')
             ->select();
         return json(['status' => 200, 'msg' =>$res]);
     }
      public function pay(){
          extract(input());
          $type = input('param.type');//1水费 2电费  3物业费  4停车费
          $order_number = date('YmdHis') . str_pad(mt_rand(1, 99999999), 6, '0', STR_PAD_LEFT).$user_id;
          $pay_id = input('param.pay_id');
          $place_id = input('param.place_id');//车位ID
          $house_id = input('param.house_id');//房产ID
          //查询这个缴费的数据
          $info = Db::table('up_xq_pay')->where('pay_id',$pay_id)->find();
          //根据房产ID查询这个房产所属小区，幢，房号，房产编号
          $res = Db::table('up_xq_house')->alias('a')
              ->join('up_xq ux','ux.xq_id = a.xq_id','left')
              ->join('up_xq_block uxb','uxb.block_id = a.block_id','left')
              ->join('up_xq_room uxr','uxr.room_id = a.room_id','left')
              ->field('a.house_num,ux.xq_name,uxb.block_name,uxr.room_name')
              ->where('a.house_id',$info['house_id'])
              ->find();

          $data['pay_house_id'] = $house_id;
          $data['pay_user_id'] = $user_id;
          $data['pay_number'] = $order_number;
          $data['pay_cate'] = $type;
          $data['pay_price'] = $info['pay_money'];
          $data['pay_time'] = date('Y-m-d H:i:s');
          $data['pay_hotel_code'] = $res['house_num'];
          $data['pay_xq'] = $res['xq_name'];
          $data['pay_lou'] = $res['block_name'];
          $data['pay_house'] = $res['room_name'];
          $data['pay_xq_id'] = $pay_id;
          if($place_id!=''){//车位ID不能为空
            $place_num = Db::table('up_xq_place')->where('place_id',$place_id)->value('place_num');
            $data['pay_place_code'] = $place_num;
          }
          $list = Db::table('up_pay')->insertGetId($data);
          return json(['status' => 200, 'msg' =>$list]);
      }
     //租金缴纳
      public function rent(){
          extract(input());
          $order_number = date('YmdHis') . str_pad(mt_rand(1, 99999999), 6, '0', STR_PAD_LEFT).$user_id;
          $money = input('param.money');
          $house_id = input('param.house_id');
          $data['pay_user_id'] = $user_id;
          $data['pay_number'] = $order_number;
          $data['pay_cate'] = 5;
          $data['pay_price'] = $money;
          $data['pay_house_id'] = $house_id;
          $data['pay_time'] = date('Y-m-d H:i:s');
          $list = Db::table('up_pay')->insertGetId($data);
          return json(['status' => 200, 'msg' =>$list]);
      }
}